package com.abbas.abchat.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.R;
import com.abbas.abchat.dao.ChatTableDao;
import com.abbas.abchat.dao.impl.ChatMsgDaoImpl;
import com.abbas.abchat.dao.impl.ChatTableDaoImpl;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.entity.ChatMsg;
import com.abbas.abchat.entity.ChatTable;
import com.abbas.abchat.utils.SqlUtils;
import com.bumptech.glide.Glide;

import java.util.Date;
import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/14 9:19
 */
public class AccountListAdapter extends BaseAdapter {

    //上下文
    private Context mContext;
    //搜索账号数据
    private List<Account> accounts;


    public AccountListAdapter(Context mContext, List<Account> accounts) {
        this.mContext = mContext;
        this.accounts = accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return accounts == null ? 0 : accounts.size();
    }

    @Override
    public Object getItem(int position) {
        return accounts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.account_item,null,false);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Account account = accounts.get(position);

        if (TextUtils.isEmpty(account.getImgPath())){
            Glide.with(mContext).load(R.drawable.ic_people_24).into(viewHolder.im);
        }else {
            Glide.with(mContext).load(account.getImgPath()).into(viewHolder.im);
        }
        if(!TextUtils.isEmpty(account.getName())){
            viewHolder.name.setText(account.getName());
        }else {
            viewHolder.name.setText(account.getAccount());
        }

        return convertView;
    }

    static class ViewHolder{
        ImageView im;
        TextView name;
    }
}
